$(function () {
    window.startDate = $('#datePicker').find('.start').html();
    window.endDate = $('#datePicker').find('.end').html();

    var loading = $('#loading');
    var toggleLoading = function (isShow) {
        if (isShow) {
            loading.removeClass('hidden');
        } else {
            loading.addClass('hidden');
        }
    };

    var tip = function (obj, info) {
        obj.tip({
            content: info,
            background: "#000",
            color: "#fff"
        });
    };

    var refreshLayout = function (datas) {
        var orderInfos = datas.item;
        if (orderInfos.length === 0) {
            $('#orderListBody').html('');
            $('#orderListBodyEmpty').removeClass('hidden');
            $('#orderListPage').addClass('hidden');
        } else {
            $('#orderListBodyEmpty').addClass('hidden');
            var templateHTML = $($('#templateOrder').clone().html());
            var ordersHTML = '';
            $.each(orderInfos, function (key, orderInfo) {
                templateHTML.find('.order-number').html(orderInfo.orderNumber);
                templateHTML.find('.order-out-number').html(orderInfo.outOrderNumber);
                templateHTML.find('.order-user-info').html(orderInfo.user.name).attr('title', orderInfo.user.name + '<br/>' + orderInfo.user.mobile);
                templateHTML.find('.order-total').html(orderInfo.price);
                templateHTML.find('.order-deposit').html(orderInfo.deposit);
                templateHTML.find('.order-paied-time').html(orderInfo.payTime);
                templateHTML.find('.order-create-time').html(orderInfo.createTime);
                var currentPayType = parseInt(orderInfo.payType);
                templateHTML.find('.order-channel').html(currentPayType === 0 ? lang('hotel.wechat') : lang('hotel.alipay'));
                ordersHTML += templateHTML.prop('outerHTML');
            });
            $('#orderListBody').html(ordersHTML);
            $('[data-toggle="tooltip"]').tooltip();
            var pageInfo = datas.page;
            var currentPage = parseInt(pageInfo.currentPage);
            var pages = parseInt(pageInfo.total);
            var pageContainer = $('#orderListPage');
            pageContainer.removeClass('hidden');
            var pageHTML = '';
            var templatePage = $($('#templatePage').clone().html());
            for (var i = 1; i <= pages; i++) {
                if (currentPage === i) {
                    templatePage.addClass('active');
                } else {
                    templatePage.removeClass('active');
                }
                templatePage.find('a').html(i).attr('data-value', i);
                pageHTML += templatePage.prop('outerHTML');
            }
            pageContainer.find('.pagination').html(pageHTML);
            pageContainer.find('.page-count').html(pages);
            pageContainer.find('.page-total').html(pageInfo.totalCount);
        }
    };
    var requestInfo = function (page,  statDate, endDate, mobile, submitBtn) {
        window.lastRequestStart = statDate;
        window.lastRequestEnd = endDate;
        window.lastRequestMobile = mobile;
        toggleLoading(true);
        var params = {
            'hotel': window.currentHotel,
            'currentPage': page,
            'startDate': statDate,
            'endDate': endDate
        };
        if (mobile !== '') {
            params.mobile = mobile;
        }
        ajaxRequest('/Home/Hotel/partnerPayItem', params, 'post', function (data) {
            toggleLoading(false);
            window.serverDatas = data.data;
            refreshLayout(window.serverDatas);
        }, function (error, obj) {
            toggleLoading(false);
            if (typeof error.responseJSON == 'undefined') {
                tip(obj,lang('tip.system'));
                return;
            }

            tip(obj, error.responseJSON.info);
        }, submitBtn);
    };

    requestInfo(1, window.startDate, window.endDate, '', $('#filterSearch'));

    $('#orderListPage .pagination').on('click', 'li:not(.active) a', function () {
        var pageIndex = $(this).data('value');
        var submitBtn = $('#filterSearch');
        requestInfo(pageIndex, window.lastRequestStart, window.lastRequestEnd, window.lastRequestMobile, submitBtn);
    });

    // 选择日期
    var format = 'YYYY-MM-DD';
    $('#datePickerInput').daterangepicker({
        'showDropdowns': true,
        'autoApply': true,
        'dateLimit': {
            'days': 30
        },
        'locale': {
            'format': format,
            'applyLabel': lang('date.ensure'),
            'cancelLabel': lang('date.cancel'),
            'daysOfWeek': [
                lang('week.7'),
                lang('week.1'),
                lang('week.2'),
                lang('week.3'),
                lang('week.4'),
                lang('week.5'),
                lang('week.6')
            ],
            'monthNames': [
                lang('month.1'),
                lang('month.2'),
                lang('month.3'),
                lang('month.4'),
                lang('month.5'),
                lang('month.6'),
                lang('month.7'),
                lang('month.8'),
                lang('month.9'),
                lang('month.10'),
                lang('month.11'),
                lang('month.12')
            ],
            'firstDay': 1
        },
        'linkedCalendars': false,
        'parentEl': 'body',
        'startDate': window.startDate,
        'endDate': $('#datePicker').find('.end').html(),
        'minDate': '2000-01-01',
        'maxDate': '2038-01-01'
    }, function (start, end, label) {
        window.startDate = start.format(format);
        window.endDate = end.format(format);
        $('#datePicker').find('.start').html(window.startDate);
        $('#datePicker').find('.end').html(window.endDate);
    });
    $('#datePicker').on('click', function () {
        $('#datePickerInput').trigger('focus');
    });

    // 查询筛选
    $('#filterSearch').on('click', function () {
        var filterMobile = $('#filterMobile').val().trim();
        if (filterMobile !== '' && !window.commonRegexp.mobile.test(filterMobile)) {
            tip($('#filterMobile'), lang('tip.mobile'));
            return;
        }
        var submitBtn = $(this);
        requestInfo(1, window.startDate, window.endDate, filterMobile, submitBtn);
    });
});
